<template>
	<view class="wrapper">
		<!-- BOTTOM TREE IMG -->
		<view class="bg-tree" style="background-image: url(http://www.huancelian.com/wxStatic/bg-tree.svg)"></view>
		<view class="content" style="background-image: url(http://www.huancelian.com/wxStatic/bg-top.svg);">
			<view class="header">
				<image class="login-logo" src="/static/logo.svg"></image>
				<text class="title">{{title}}</text>
			</view>
			<text class="welcome">Welcome</text>
			<text class="please-login">{{pleaseLogin}}</text>
			<view class="inputs">
				<input type="text" v-model="loginForm.username" maxlength="35" :placeholder="accountPlaceholder" />
			</view>
			<view class="inputs">
				<input type="text" v-model="loginForm.password" maxlength="35" :password="true"
					:placeholder="passwordPlaceholder" />
			</view>
			<button class="button_login" @click="switchTabToTask">{{login}}</button>
			<view class="wx-login">
				<text class="wx-login-text" @click="navigateBack">{{wxLogin}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '环测链',
				login: '登录',
				pleaseLogin: '请登录',
				wxLogin: '微信登录',
				accountPlaceholder: '请输入您的账号',
				passwordPlaceholder: '请输入您的密码',
				loginForm: {
					username: '',
					password: ''
				},
				id: "",
				name: "",
				intersection: "",
				inCode: "",
				scanTheCode: "",
				type: "",
				inScanThe: "",
				roles: [],
				arr: [
					"TeA",
					"sbcj",
					"KS",
					"Po",
				],
				arr1: [
					"sbcj",
					"TeA",
				],
				arr2: [
					"KS",
					"Po",
				]
			}
		},
		onLoad(option) {
			this.id = option.id;
			this.scanTheCode = option.scanTheCode
			this.type = option.type
		},

		methods: {
			switchTabToTask() {
				if (this.loginForm.username !== '' && this.loginForm.password !== '') {
					this.$api.login(this.loginForm, async res => {
						if (res.success) {
							this.$common.successToShow("登录成功")
							this.$db.set('userToken', res.result);
							this.$db.Subscribe()
							uni.removeStorage('SIGN');
							await this.getUserInfo();
						} else {
							// this.$common.errorToShow("登录条件不满足")
							uni.showToast({
								title: res.message,
								icon: 'none',
							})
						}
					})
				} else {
					this.$common.errorToShow("请录入登录信息...");
				}
			},
			getUserInfo() {
				this.$api.getUserInfo(res => {
					if (res.success && res.result) {
						this.name = res.result.empName
						this.roles = res.result.roles
						this.$store.commit('SAVE_CUR_USER', res.result);
						if (res.result.roles.length > 0) {
							this.$store.commit('SAVE_ROLE_LISTS', res.result.roles);

							// 交集
							this.intersection = this.arr.filter(function(val) {
									return res.result.roles.indexOf(val) > -1
								}),

								// // 交集
								this.inCode = this.arr1.filter(function(val) {
									return res.result.roles.indexOf(val) > -1
								}),

								// 交集
								this.inScanThe = this.arr2.filter(function(val) {
									return res.result.roles.indexOf(val) > -1
								})
								switch (this.intersection[0]) {
									case 'KS':
										this.$store.commit('SAVE_CUR_ROLE', {
											roleId: "KS",
											roleName: '采样员'
										});
										break
									case 'Po':
										this.$store.commit('SAVE_CUR_ROLE', {
											roleId: "Po",
											roleName: '采样组长'
										});
										break
									case 'sbcj':
										this.$store.commit('SAVE_CUR_ROLE', {
											roleId: "sbcj",
											roleName: '仪器设备管理员'
										});
										break
									case 'TeA':
										this.$store.commit('SAVE_CUR_ROLE', {
											roleId: "TeA",
											roleName: '分析员'
										});
										break
									default:
										this.$store.commit('SAVE_CUR_ROLE', {});
								}
						} else {
							this.$store.commit('SAVE_ROLE_LISTS', res.result.roles);

						}

						if (this.intersection[0] == 'TeA') {
							wx.navigateTo({
								url: '/pages/maintenance/index?id=' + this.id + "&empName=" + this
									.name + "&scanTheCode=" + this.scanTheCode
							})
						}
						if (this.intersection[0] == 'sbcj') {
							console.log("成功")
							wx.reLaunch({
								url: '/pages/instrument/index?id=' + this.id +
									"&empName=" + this
									.name + "&scanTheCode=" + this.scanTheCode + '&sbcj=' + "sbcj"
							})
						}
						if (this.intersection[0] == 'KS' || this.intersection[0] == 'Po') {
							uni.reLaunch({
								url: '/pages/mine/index?id=' + this.id +
									"&empName=" + this
									.name + "&scanTheCode=" + this.scanTheCode
							});
						}

						if (this.intersection.length == 1) {
							if (this.intersection[0] == 'KS') {
								wx.setStorageSync('sweepValue', "scanCode");
							}
							if (this.intersection[0] == 'Po') {
								wx.setStorageSync('sweepValue', "sweepKs");
								wx.setStorageSync('todo', "0");
							}
							if (this.intersection[0] == 'sbcj') {
								wx.setStorageSync('sweepValue', "sweep");
							}
						}



						if (this.type == 3 || this.type == 4 || this.type == 5) {
							if (this.scanTheCode != "") {
								if (this.inCode[0] == 'sbcj') {
									wx.reLaunch({
										url: '/pages/warehouse/index?id=' + this.id +
											"&empName=" + this
											.name + '&sbcj=' + "sbcj"
									})
								}
							} else {
								if (this.inCode[0] == 'sbcj') {
									wx.reLaunch({
										url: '/pages/InstrumentManagement/index?id=' + this.id +
											"&empName=" + this
											.name + '&sbcj=' + "sbcj"
									})
								}
							}
						}


						if (this.type == 1 || this.type == 2) {
							if (this.scanTheCode != "") {
								if (this.inScanThe[0] == 'KS' || this.inScanThe[0] == 'Po') {
									uni.reLaunch({
										url: '/pages/claim/index?id=' + this.id +
											"&empName=" + this
											.name
									});
								}
							} else {
								if (this.inScanThe[0] == 'KS' || this.inScanThe[0] == 'Po') {
									uni.reLaunch({
										url: '/pages/mine/index?id=' + this.id +
											"&empName=" + this
											.name
									});
								}
							}
						}
					} else {
						this.$common.errorToShow(res.result)
					}
				})
			},
			navigateBack() {
				uni.navigateBack();
			},
		}
	}
</script>

<style>
	uni-page-body {
		height: 100%;
	}

	.wrapper {
		width: 100%;
		height: 100vh;
		overflow: hidden;
		background-color: #F0F2FA;
	}

	.wrapper .login-logo {
		height: 140rpx;
		width: 140rpx;
	}

	.content {
		position: relative;
		top: 0;
		left: 0;
		/* background-image: url(../../static/bg-top.svg); */
		background-position: 40% 50%;
		width: 100%;
		height: 0;
		padding-bottom: 205%;
		background-repeat: no-repeat;
		background-size: 175% 175%;
	}

	.header {
		padding-top: 17%;
		width: 100%;
		height: 84rpx;
		margin-bottom: 72rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.title {
		font-size: 47rpx;
		color: #193566;
	}

	.welcome {
		width: 100%;
		display: block;
		padding-left: 100rpx;
		font-size: 70rpx;
		font-weight: 500;
		color: #193566;
	}

	.please-login {
		margin-top: 5rpx;
		display: block;
		width: 100%;
		padding-left: 100rpx;
		font-size: 35rpx;
		font-weight: 400;
		color: #193566;
	}

	.inputs {
		margin-top: 48rpx;
		height: 110rpx;
		line-height: 110rpx;
		padding: 0 100rpx;
	}

	.inputs input {
		height: 110rpx;
		line-height: 110rpx;
		padding: 0rpx 0rpx 0rpx 70rpx;
		background: #F0F2FA;
		box-shadow: inset 5rpx 0rpx 11rpx 2rpx rgba(0, 0, 0, 0.16);
		border-radius: 65rpx 65rpx 65rpx 65rpx;
	}

	/deep/ .uni-input-wrapper {
		display: flex;
		flex: 1;
		width: auto;
		height: 130rpx;

	}

	/deep/ .uni-input-input {
		margin-left: 56rpx;
	}

	.input-placeholder {
		width: 100%;
		height: 100%;
		color: #A9B6C9;
		font-size: 33rpx;
		margin-left: 56rpx;
	}

	.button_login {
		margin-top: 61rpx;
		display: block;
		width: 390rpx;
		height: 115rpx;
		padding-left: 40rpx;
		padding-top: 0rpx;
		font-size: 45rpx;
		background-color: #193566;
		color: #fff;
		box-shadow: 0px 5px 11px 2px rgba(0, 0, 0, 0.16);
		border-radius: 65px 65px 65px 65px;
		letter-spacing: 8rpx;
	}

	.wx-login {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 63rpx;
		color: #193566;
		opacity: 0.8;
	}

	.wx-login-text {
		font-size: 37rpx;
		color: #193566;
		text-decoration: underline;
		letter-spacing: 0rpx;
		font-weight: 400;
		/* opacity: 0.7; */
	}

	.bg-tree {
		position: absolute;
		bottom: 9%;
		left: 5%;
		/* background-image: url(../../static/bg-tree.svg); */
		/* background-position: 40% 50%; */
		width: 100%;
		height: 0;
		padding-top: 69%;
		background-repeat: no-repeat;
		background-size: 140% 140%;
	}
</style>